import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_ai_dart/routers/app_pages.dart';
import 'package:smart_ai_dart/utils/storage.dart';
import 'package:smart_ai_dart/utils/themes.dart';
import 'package:smart_ai_dart/utils/translate.dart';

class ProfileController extends GetxController {
  final isLogin = false.obs;
  final isDarkMode = false.obs;

  // 新增用户信息相关响应式变量
  final avatarUrl = ''.obs;
  final nickname = '用户名'.obs;
  final email = 'user@example.com'.obs;

  @override
  void onInit() {
    super.onInit();
    isDarkMode.value = AppTheme.shared.themeMode == ThemeMode.dark;
    _loadUserProfile();
  }

  void _loadUserProfile() {
    // TODO: 从本地存储或网络获取用户信息
    avatarUrl.value = 'https://example.com/avatar.png';
    nickname.value = '张三';
    email.value = 'zhangsan@example.com';
  }

  void changeAvatar() async {
    // TODO: 实现头像选择和上传逻辑
    final result = await Get.dialog(
      AlertDialog(
        title: Text('change_avatar'.tr),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ListTile(
              title: Text('take_photo'.tr),
              onTap: () {
                // 拍照逻辑
                Get.back(result: 'camera');
              },
            ),
            ListTile(
              title: Text('choose_from_album'.tr),
              onTap: () {
                // 相册选择逻辑
                Get.back(result: 'album');
              },
            ),
          ],
        ),
      ),
    );
    // 处理选择结果
  }

  void editProfile() {
    Get.toNamed(Routes.EDIT_PROFILE);
  }

  void toggleLanguage() {
    final locale = Get.locale;
    if (locale?.languageCode == 'en') {
      TranslateService.switchLanguage(TranslateService.zhLocale);
    } else {
      TranslateService.switchLanguage(TranslateService.enLocale);
    }
  }

  void toggleTheme() async {
    await AppTheme.shared.toggleTheme();
    isDarkMode.value = AppTheme.shared.themeMode == ThemeMode.dark;
  }

  void login() => isLogin.value = true;

  void logout() async {
    await StorageUtils.shared.remove('jwt_token');
    Get.offAllNamed(Routes.LOGIN);
  }
}
